$(document).ready(function() {
    if(sessionStorage.getItem("msg_data")==null){
        alert("您的登录信息已过期请重新登陆");
        window.location.href="http://172.18.80.9:8087/admin/login/login"
    }
    var msg_data =JSON.parse(sessionStorage.getItem("msg_data"));
    console.log(msg_data.id);
    console.log(msg_data.token);
    // 分页
    var setTotalCount = 301;
    $('#box').paging({
        initPageNo: 3, // 初始页码
        totalPages: 30, //总页数
        totalCount: '合计' + setTotalCount + '条数据', // 条目总数
        slideSpeed: 600, // 缓动速度。单位毫秒
        jump: true, //是否支持跳转
        callback: function(page) { // 回调函数
            // console.log(page);
        }
    })
    // 申报项目列表
    $.ajax({
        type: "POST",
        dataType:"json",
        data:{uid:msg_data.id,token:msg_data.token},
        url: "http://172.18.80.9:8087/admin/principal/list",
        success: function (data) {
            if(data.code==9){
                alert(data.msg)
                window.location.href="http://172.18.80.9:8087/admin/login/login"
                return false;
            }
        console.log(data.data)
         var html=""
            for(var tmp of data.data){
                if(tmp.begin_time!=null){
                    var begintime=tmp.begin_time.split(' ')[0]
                }else {
                    var begintime="";
                }
                if(tmp.end_time!=null){
                    var endtime=tmp.end_time.split(' ')[0]
                }else{
                    var endtime="";
                }
                if(tmp.total_money==null){
                    var  money="";
                }else{
                    var money=tmp.total_money;
                }
             html+=`
                 <tr>
                    <td>${tmp.item_no}</td>
                    <td>${tmp.project_type}</td>
                    <td>${tmp.project_name}</td>
                    <td>${begintime}</td>
                    <td>${endtime}</td>
                    <td>${tmp.belong_company}</td>
                    <td>${money}</td>
                    <td>
                        <div class="btnn">
                            <a href="#" class="btn btn-info" style="background-color: #416aff;border-color:#416aff;"  id="editor" data-id="${tmp.id}">
                                <img src="../images/bianji.png">
                            </a>
                            <a href="">&nbsp;&nbsp;</a>
                            <a href="#" class="btn btn-info" style="background-color: #fe5722;border-color:#fe5722;" id="delect" data-id="${tmp.id}">
                                <img src="../images/delect.png">
                            </a>
                        </div>
                    </td>
                </tr>
             `
            }
            $("#tbody1").html(html);
        },
        error: function (err) {
            console.log(err);
            alert("网络故障请检查");
        }
    });
    // 查询项目
    $(document).on("click","#search",function (e) {
        e.preventDefault();
        var project_name=$("#select").val();
        $.ajax({
            type: "POST",
            dataType:"json",
            data:{project_name:project_name,uid:msg_data.id,token:msg_data.token},
            url: "http://172.18.80.9:8087/admin/principal/search",
            success: function (data) {
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                console.log(data.data);

                var html=""
                for(var tmp of data.data){
                    var begintime=tmp.begin_time.split(' ')[0]
                    var endtime=tmp.end_time.split(' ')[0]
                    html+=`
             <tr>
                <td>${tmp.item_no}</td>
                <td>${tmp.project_type}</td>
                <td>${tmp.project_name}</td>
                <td>${begintime}</td>
                <td>${endtime}</td>
                <td>${tmp.belong_company}</td>
                <td>${tmp.total_money}</td>
                <td>
                    <div class="btnn">
                        <a href="#" class="btn btn-info" style="background-color: #416aff;border-color:#416aff;"  id="editor" data-id="${tmp.id}">
                            <img src="../images/bianji.png">
                        </a>
                        <a href="">&nbsp;&nbsp;</a>
                        <a href="#" class="btn btn-info" style="background-color: #fe5722;border-color:#fe5722;" id="delect" data-id="${tmp.id}">
                            <img src="../images/delect.png">
                        </a>
                    </div>
                </td>
            </tr>
         `
                }
                $("#tbody1").html(html);

            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    })
    //添加项目跳转
    $(document).on("click","#addproject",function(){
        $.ajax({
            type: "POST",
            dataType:"json",
            data:{uid:msg_data.id,token:msg_data.token},
            url: "http://172.18.80.9:8087/admin/principal/add",
            success: function (data){
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                     window.location.href="http://172.18.80.9:8087/admin/principal/addproject?"+"add="+encodeURI(data.data);
            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    })
    // 操作删除按钮
    $(document).on("click","#delect",function (e) {
        e.preventDefault();
        var did =$(this).data("id");
        console.log(did);
        $.ajax({
            type: "POST",
            dataType:"json",
            data:{id:did,uid:msg_data.id,token:msg_data.token},
            url: "http://172.18.80.9:8087/admin/principal/delproject",
            success: function (data) {
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                console.log(data);
              location.reload();
            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    })
    //编辑项目按钮
    $(document).on("click","#editor",function (e) {
        e.preventDefault();
        var eid =$(this).data("id");
        window.location.href="http://172.18.80.9:8087/admin/principal/addproject?"+"id="+encodeURI(eid);
    })
})



